// pages/person/list.js
var core = require('../../utils/core.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 200,
    index:0,
    visite_page:1,
    visite:[],
    visite_fresh:false,

    userlist_page:1,
    userlist:[],
    userlist_fresh:false,

    group_page:1,
    group:[],
    group_fresh:false,
  },
  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },

  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },

  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.getProjectList()
     this.getGroupList()
     this.getReportList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  more(){
   console.log(1111)
   //我的项目
   if(this.data.index==0){
      this.getProjectList()
   }
   if(this.data.index==2){
    this.getGroupList()
 }
},
onRefresh(){
  //我的项目
  if(this.data.index==0){
    this.setData({
      visite_page:1,
      visite:[],
      visite_fresh:true
    })
     this.getProjectList()
  }
   //我的报告
   if(this.data.index==1){
    this.setData({
      userlist_page:1,
      userlist:[],
      userlist_fresh:true,
    })
    this.getReportList()
  }
  //我的群
  if(this.data.index==2){
    this.setData({
      group_page:1,
      group:[],
      group_fresh:true,
    })
    this.getGroupList()
  }
},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 
  //选项卡点击
  change:function(e){
    this.setData({index:e.target.id})
},
//触摸滑动
touch_change:function(e){
  // console.log(e)
  // return
  this.setData({index:e.detail.current})
},
/**
 * 项目list
 */
getProjectList:function(){
  //this.data=this.data1
  
  var self=this
  core.post("ProjectList",{page:self.data.visite_page,is_my:1},function(res){
    console.log(res)
    if(res.code==1){
        
        var current=self.data.visite
        for(var i=0;i<res.data.length;i++){
          current.push(res.data[i])
        }
        self.setData({visite:current,visite_fresh:false,visite_page:self.data.visite_page+1})
    }
  })
  
  console.log(1)     
},
/***
 * 我的报告
 */
getReportList:function(){
//this.data=this.data1
  
  var self=this
  core.post("report",{page:self.data.userlist_page,is_my:1,op:'list'},function(res){
    console.log(res)
    if(res.code==1){
        
        var current=self.data.userlist
        for(var i=0;i<res.data.length;i++){
          current.push(res.data[i])
        }
        self.setData({userlist:current,userlist_fresh:false,userlist_page:self.data.userlist_page+1})
    }
  })

  console.log(1)     
},
/**
 * 群list
 */
getGroupList:function(){
  //this.data=this.data1
  
  var self=this
  core.post("GroupList",{page:self.data.group_page,is_my:1},function(res){
    console.log(res)
    if(res.code==1){
        
        var current=self.data.group
        for(var i=0;i<res.data.length;i++){
          current.push(res.data[i])
        }
        self.setData({group:current,group_fresh:false,group_page:self.data.group_page+1})
    }
  })
  
  console.log(1)     
},
/**
 * 项目删除
 */
ProjectDel:function(target){
  var targetData=target.target.id.split('&') 
  var id=targetData[0]
  var index=targetData[1]
  var self=this
  wx.showModal({
    title: '提示',
    content: '确定要删除吗？',
    success: function (res) {
      if(res.confirm){
        core.post("ProjectDel",{id:id},function(res){
          if(res.code==1){
              var current=self.data.visite
              current.splice(index*1,1)
              self.setData({visite:current})
          }
        })
      }
    }
  })
},
/**
 * 报告删除
 */
ReportDel:function(target){
  var targetData=target.target.id.split('&') 
  var id=targetData[0]
  var index=targetData[1]
  var self=this
  wx.showModal({
    title: '提示',
    content: '确定要删除吗？',
    success: function (res) {
      if(res.confirm){
        core.post("Report",{id:id,op:'del'},function(res){
          if(res.code==1){
              var current=self.data.userlist
              current.splice(index*1,1)
              self.setData({userlist:current})
          }
        })
      }
    }
  })
},
/**
 * 群删除
 */
GroupDel:function(target){
  var targetData=target.target.id.split('&') 
  var id=targetData[0]
  var index=targetData[1]
  var self=this
  wx.showModal({
    title: '提示',
    content: '确定要删除吗？',
    success: function (res) {
      if(res.confirm){
        core.post("GroupDel",{id:id},function(res){
          if(res.code==1){
              var current=self.data.group
              current.splice(index*1,1)
              self.setData({group:current})
          }
        })
      }
    }
  })
},
/**
 * 置顶服务
 */
top:function(target){
  var targetData=target.target.id.split('&') 
  wx.navigateTo({
    url:"/pages/release/top?id="+targetData[0]+"&title="+targetData[1]+"&type="+targetData[2]
  })
},
/**
 * 项目刷新服务
 */
reload:function(target){
  console.log(target)
  core.post("ProjectReload",{id:target.target.id},function(res){
    if(res.code==1){
      wx.showToast({
        'title':'刷新成功',
        'icon':'success'
      })
    }else{
      wx.showToast({
        'title':res.msg,
        'icon':'none'
      })
    }
  })
},
Reportreload:function(target){
  console.log(target)
  core.post("Report",{id:target.target.id,op:'reload'},function(res){
    if(res.code==1){
      wx.showToast({
        'title':'刷新成功',
        'icon':'success'
      })
    }else{
      wx.showToast({
        'title':res.msg,
        'icon':'none'
      })
    }
  })
},
/**
 * 群刷新服务
 */
groupreload:function(target){
  core.post("GroupReload",{id:target.target.id},function(res){
    if(res.code==1){
      wx.showToast({
        'title':'刷新成功',
        'icon':'success'
      })
    }else{
      wx.showToast({
        'title':res.msg,
        'icon':'none'
      })
    }
  })
},
//上线，下线
line:function(target){
  var targetData=target.target.id.split('&');
  var self=this 
  if(targetData[1]==1){
     var index_data='visite'
  }
  if(targetData[1]==2){
    var index_data='userlist'
 }
 if(targetData[1]==3){
  var index_data='group'
}
var index=targetData[3]
  core.post("Line",{id:targetData[0],type:targetData[1]},function(res){
    if(res.code==1){
        var data=self.data[index_data]
        data[index].status=targetData[2]
        self.setData({[index_data]:data})
    }else{
      wx.showToast({
        'title':res.msg,
        'icon':'none'
      })
    }
  })
}

})